package com.orangelife.mobile.address.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.sp.ShoppingSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.address.activity.MakeAddressListActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

import java.util.Map;

/**
 * 收货地址通用界面
 * Create by cfr .Modify by wgf on 160225.
 * 此次修改内容：增加注释
 *
 * @author cfr
 */
public class AddressFragment extends Fragment {

    private View homePageView;
    private TextView username;
    private TextView userPhone;
    private TextView address;
    private LinearLayout ll;
    private RelativeLayout rladdress;

    private Map<String, Object> map;
    private LinearLayout addresslist;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (homePageView == null) {
            homePageView = inflater.inflate(R.layout.activity_address,
                    container, false);
            initView();
            findView();
        }
        /**
         * 缓存的viewiew需要判断是否已经被加过parent，
         * 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
         */
        ViewGroup parent = (ViewGroup) homePageView.getParent();
        if (parent != null) {
            parent.removeView(homePageView);
        }

        return homePageView;
    }

    private void initView() {
        username = (TextView) homePageView.findViewById(R.id.tv_username);
        userPhone = (TextView) homePageView.findViewById(R.id.tv_userphone);
        address = (TextView) homePageView.findViewById(R.id.tv_address);
        ll = (LinearLayout) homePageView.findViewById(R.id.ll_fill_address);
        rladdress = (RelativeLayout) homePageView.findViewById(R.id.rl_address);

        addresslist = (LinearLayout) homePageView.findViewById(R.id.llClick);
    }

    private void findView() {
        ll.setVisibility(View.VISIBLE);
        rladdress.setVisibility(View.GONE);
        if (!StringUtil.isContainBlank(ShoppingSp.getTradeName(), ShoppingSp.getTradePhone(), ShoppingSp.getTradeAddress())) {
            initUserInfo();
        } else {
            JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ADDRESS_DEFAULT_URL, null, handler, 0, HttpUtil.ACTION_URI);
        }
        addresslist.setOnClickListener(onClick);
    }

    public void onResume() {
        super.onResume();
        initUserInfo();
    }

    private void initUserInfo() {
        String consignee = ShoppingSp.getTradeName();
        String phone = ShoppingSp.getTradePhone();
        String address2 = ShoppingSp.getTradeAddress();
        if (!StringUtil.isContainBlank(consignee, phone, address2)) {
            username.setText(consignee);
            userPhone.setText(phone);
            address.setText(address2);
            ll.setVisibility(View.GONE);
            rladdress.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 打开地址列表
     *
     * @param view
     */
    View.OnClickListener onClick = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), MakeAddressListActivity.class);
            getActivity().startActivityForResult(intent, getActivity().RESULT_FIRST_USER);
        }
    };
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 500:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    ll.setVisibility(View.VISIBLE);
                    rladdress.setVisibility(View.GONE);

                    break;
                case 1001:
                    ll.setVisibility(View.VISIBLE);
                    rladdress.setVisibility(View.GONE);
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
                case 0:
                    map = JSONHelper.jsonToMap(msg.obj.toString());
                    if (map.get("errCode").toString().equals("0")) {
                        map = JSONHelper.jsonToMap(map.get("entity").toString());
                        if (map != null) {
                            username.setText(map.get("consignee").toString());
                            userPhone.setText(map.get("phone").toString());
                            address.setText(map.get("addressJoin").toString());
                            ShoppingSp.setTradeAddressInfo(
                                    String.valueOf(map.get("consignee")),
                                    String.valueOf(map.get("phone")),
                                    String.valueOf(map.get("addressJoin")),
                                    String.valueOf(map.get("addressID")),
                                    String.valueOf(map.get("provinceCode")),
                                    String.valueOf(map.get("cityCode")),
                                    String.valueOf(map.get("districtID")));
                            ll.setVisibility(View.GONE);
                            rladdress.setVisibility(View.VISIBLE);
                        }
                    } else {
                        ll.setVisibility(View.VISIBLE);
                        rladdress.setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }
        }

    };

}
